apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", '../../appJSON'], function(V, $api, $, app, service, winURL) {
        app.ready()
        var vm = {
            el: '.container',
            data: {
                cardInfo: '',
                logo: '', // 店铺logo
                shop_intro: '', // 店铺简介
                shopId: '', // 商店编号
                shopName: '', // 商店名称
                location: '', // 商店地址
                location_id_obj: {
                    province_id: '',
                    city_id: '',
                    location_id: ''
                }, //店铺店址ID对象
                imgarr: []
            },
            created: function() {
                var that = this;

                this.init(function() {
                    that.getBanner();
                });
                api.addEventListener({
                    name: 'keyboardshow'
                }, function(ret, err) {
                    $('.header').css({
                        'top': 0,
                        'position': 'static'
                    });
                });

                api.addEventListener({
                    name: 'keyboardhide'
                }, function(ret, err) {
                    $('.header').css({
                        'top': 0,
                        'position': 'fixed'
                    });
                });
            },
            mounted: function() {
                $(".cardExplain").on("input", function() {
                    this.value = this.value.replace(/\ud83d[\udc00-\ude4f\ude80-\udfff]/g, '');
                });
                var that = this;
                // 监听是否选中了商品
                api.addEventListener({
                    name: 'goodsId'
                }, function(ret, err) {
                    if (ret.value) {
                        if (ret.value.goodsId) {
                            api.toast({
                                msg: '关联成功',
                                duration: 2000,
                                location: 'middle'
                            });
                        }
                        var arr = that.imgarr;

                        // 选中的图片索引
                        var index = ret.value.index;

                        // 商品id
                        var goodsId = ret.value.goodsId;
                        var goodsName = ret.value.goodsName;

                        // 修改数组内容
                        that.$set(that.imgarr[index], 'name', goodsName);
                        that.$set(that.imgarr[index], 'link_type_id', goodsId);
                    } else {
                        api.toast({
                            msg: '关联失败',
                            duration: 2000,
                            location: 'middle'
                        });
                    }
                });

                // 保存修改信息并刷新我的店铺页面
                api.addEventListener({
                    name: 'saveMsg'
                }, function(ret, err) {
                    if (ret) {
                        that.saveCard();
                    }
                });
            },

            methods: {
                init: function(callback) {
                    // 获取店铺信息
                    var that = this;
                    var shopId = api.pageParam.shopId;
                    this.shopId = shopId;
                    var unionId = $api.getStorage("shop_unionid");
                    app.ajax(
                        service['shopDetail'], {
                            data: {
                                values: {
                                    unionid: unionId,
                                    userid: $api.getStorage('user').data.out_user_id
                                }
                            }
                        },
                        function(res) {
                            that.cardInfo = res.data;
                            that.logo = res.data.shop_logo;
                            that.shopName = res.data.shop_name;
                            that.shop_intro = res.data.shop_intro;
                            that.location = res.data.shop_location;
                            that.$nextTick(function(){
                              $('.container').slideDown()
                            })
                            callback();
                        }
                    );
                },

                // 获取自己店铺的banner列表
                getBanner: function() {
                    var that = this;
                    app.ajax(
                        service['bannerList'], {
                            data: {
                                values: {
                                    unionid: $api.getStorage("shop_unionid"),
                                    shop_code: that.cardInfo.shop_code
                                }
                            }
                        },
                        function(res) {
                            if (res.code == 0) {
                                that.imgarr = res.data;
                            } else {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }
                    );
                },

                // 上传图片
                getPictureS: function() {
                    var that = this;
                    if (that.imgarr.length >= 1) {
                        api.toast({
                            msg: '最多选择一张图片',
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                    app.getPictureS(1, this.imgarr.length, service['fileupload'], function(res) {
                        if (res.length > 0) {
                            api.toast({
                                msg: '上传成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            if (that.imgarr.length >= 1) {
                                that.imgarr = res;
                            } else {
                                for (var i = 0, len = res.length; i < len; i++) {
                                    var obj = {
                                        name: '',
                                        url: res[i]
                                    };
                                    that.imgarr.push(obj);
                                }
                            }
                        }
                    })
                },

                // 保存
                saveCard: function() {
                    var that = this;
                    var shopName = $('.shop_name').val();
                    var cardExplain = $('.cardExplain').val();

                    var unionId = $api.getStorage("shop_unionid");
                    var shop_code = this.shopId;
                    var shop_address = $('.address-input').val();
                    var reg = /\ud83d[\udc00-\ude4f\ude80-\udfff]/g;
                    if (reg.test(shopName) || reg.test(cardExplain)) {
                        api.toast({
                            msg: '禁止输入表情',
                            duration: 2000,
                            location: 'middle'
                        });
                        return;
                    }

                    if (shopName == '' || shopName == null) {
                        api.toast({
                            msg: '请填写商铺名称',
                            duration: 2000,
                            location: 'middle',
                        });
                        return false;
                    }
                    if (shopName.length > 15) {
                        api.toast({
                            msg: '店名不能超过15个字',
                            duration: 2000,
                            location: 'middle'
                        });
                        return;
                    }
                    if (shop_address == '' || shop_address == null) {
                        api.toast({
                            msg: '请选择店铺地址',
                            duration: 2000,
                            location: 'middle',
                        });
                        return false;
                    }
                    if (cardExplain == '' || cardExplain == null) {
                        api.toast({
                            msg: '请填写店铺简介',
                            duration: 2000,
                            location: 'middle',
                        });
                        return false;
                    }
                    if (that.imgarr.length == 0) {
                      api.toast({
                          msg: '请上传店铺展示图片',
                          duration: 2000,
                          location: 'middle',
                      });
                      return false;
                    }

                    // 编辑店铺信息
                    app.ajax(
                        service['shopEdit'], {
                            data: {
                                values: {
                                    appid: app.appid,
                                    userid: $api.getStorage('user').data.out_user_id,
                                    unionid: unionId,
                                    shop_code: shop_code,
                                    shop_name: shopName,
                                    shop_intro: cardExplain,
                                    shop_logo: that.logo,
                                    shop_location: shop_address,
                                    province_id: that.location_id_obj.province_id,
                                    city_id: that.location_id_obj.city_id,
                                    location_id: that.location_id_obj.location_id
                                }
                            }
                        },
                        function(res) {
                            if (res.code == 0) {

                                    that.saveShopImg(function(ret) {
                                        if (ret.code == 0) {
                                            api.toast({
                                                msg: '店铺信息修改成功',
                                                duration: 2000,
                                                location: 'middle'
                                            });
                                            if (api.pageParam.page_from != undefined) {
                                                api.openWin({
                                                    name: 'index',
                                                    // customRefreshHeader: "UIPullRefreshMotive",
                                                    url: 'widget://src/Shop/shop_index/shop_index.html',
                                                    pageParam: {

                                                    }
                                                })
                                            } else {
                                                api.closeWin();
                                            }
                                        } else {
                                            api.toast({
                                                msg: ret.message,
                                                duration: 2000,
                                                location: 'middle'
                                            });
                                        }

                                    })

                            } else {
                                api.toast({
                                    msg: res,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            }
                        });
                },
                // 添加商品链接
                addLink: function(e) {
                    var that = this;
                    var shopId = this.shopId;

                    var index = e.target.dataset.index; // 选中的图片

                    app.openWin('shopCard_Link', {
                        index: index,
                        shopId: shopId
                    });
                },
                // 选择商品
                selectGoods: function(e) {

                },
                // 移除图片
                delImg: function(e) {
                    var index = e.target.dataset.index;
                    var arr = this.imgarr;
                    arr.splice(index, 1);
                    this.imgarr = arr;
                },
                closeGoodsId: function() {
                    this.shoGoodsId = false;
                },
                // 保存图片商品链接
                saveShopImg: function(callback) {
                    var that = this
                    var imgList = this.imgarr;
                    var unionId = $api.getStorage("shop_unionid");
                    var shop_code = this.shopId;

                    // 设置图片信息
                    var new_arr = [];
                    var imgarr = that.imgarr;
                    imgarr.forEach(function(item, index) {
                        var ls;
                        if (item.link_type_id) {
                            ls = 'name^' + item.name + '^url^' + item.url + '^position^0^link_type^1^link_type_id^' + item.link_type_id + '|';
                        } else {
                            ls = 'name^ ^url^' + item.url + '^position^0^link_type^1^link_type_id^0|';
                        }
                        new_arr.push(ls);
                    });
                    var str = '';
                    for (var i = 0, len = new_arr.length; i < len; i++) {
                        str += new_arr[i];
                    }

                    app.ajax(
                        service['addBatch'], {
                            data: {
                                values: {
                                    unionid: unionId,
                                    shop_code: shop_code,
                                    batch_str: str
                                }
                            }
                        },
                        function(res) {
                            callback(res);
                        }
                    );
                },
                //--名片logo
                cardLogo: function(et) {
                    // 设置店铺logo
                    var that = this;
                    app.getPictureS(2, 1, service['fileupload'], function(res) {
                        that.logo = res[0];
                    })

                },
                //--店铺简介
                cardExplain: function(et) {},
                // 选择地址
                choicAddress: function() {
                    var that = this;
                    app.openCitySelect();
                    app.getCitySelect(function(res) {
                        var location = res.data.full_name;
                        $('.address-input').val(location);
                        that.location = res.data.full_name;
                        that.location_id_obj = {
                            province_id: res.data.province_id,
                            city_id: res.data.city_id,
                            location_id: res.data.district_id
                        }
                    })
                }
            }
        };
        $('.right-btn').on('click', function() {
            api.sendEvent({
                name: 'saveMsg'
            });
        });
        var even = function() {

        }
        var newVue = new Vue(vm);
        even();

    })
}
